from datetime import date, timedelta
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.users.models import User

# 日活用户量统计
class DailyActiveCountView(APIView):
    def get(self, request):
        # 获取当前时间
        ntime = date.today()
        # 获取日活跃量
        count = User.objects.filter(last_login__gte=ntime).count()

        return Response({'count':count, 'date': ntime})


# 日下单用户
class DailyOrderCountView(APIView):
    def get(self, request):
        today = date.today()
        count = User.objects.filter(orderinfo__create_time__gte=today).count()
        return Response({'count':count, 'date': today})


# 月增用户统计
class MonthAddCountView(APIView):
    def get(self, request):
        # 前端需要的数据形式
        data = []
        # 确定今天日期
        today = date.today()
        # 确定30天前的日期
        start_date = today - timedelta(days=30)
        # 遍历获取每一天的用户增量
        for i in range(30):
            # 当天
            currtime = start_date + timedelta(days=i)
            # 后一天
            aftertime = start_date + timedelta(days=i+1)

            # 获取这天注册用户的个数
            count = User.objects.filter(date_joined__gte=currtime, date_joined__lt=aftertime).count()

            data.append({
                'count':count,
                'date':currtime
            })

        return Response(data)


# 用户总量统计
class UserCountView(APIView):
    def get(self, request):
        ntime = date.today()
        count = User.objects.all().count()
        return Response({'count':count, 'date': ntime})

# 日增用户统计
class DayIncrementCountView(APIView):
    def get(self, request):
        ntime = date.today()
        count = User.objects.filter(date_joined__gte=ntime).count()
        return Response({'count': count, 'date': ntime})












